﻿#region Copyright (C) 2008, 2009 StreamTv

/* 
 *      Copyright (C) 2008, 2009 StreamTv
 *      http://code.google.com/p/mpstreamtv/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TvControl;

namespace ConsoleTestTool
{
    class Program
    {
        static void Main(string[] args)
        {
            #region Setup Debug System
            // Setup debug console.
            System.Diagnostics.TextWriterTraceListener myWriter = new
                System.Diagnostics.TextWriterTraceListener(Console.Out);
            System.Diagnostics.Debug.Listeners.Add(myWriter);
            System.Diagnostics.Debug.AutoFlush = true;

            System.Diagnostics.Debug.Print("Started listening to debugger.");
            System.Diagnostics.Debug.Flush();

            foreach (string argument in args)
            {
                if (argument.Contains("/hostname="))
                {
                    RemoteControl.HostName = argument.Substring(10).Trim();
                }
            }
            if (!RemoteControl.IsConnected)
            {
                System.Diagnostics.Debug.Print("Remoting: Not Connected");
                RemoteControl.HostName = "localhost";
                //RemoteControl.HostName = "192.168.1.229";
                if (RemoteControl.IsConnected)
                    System.Diagnostics.Debug.Print("Remoting: Is Connected");
                else
                    System.Diagnostics.Debug.Print("Remoting: Still Disconnected");
            }
            else
            {
                System.Diagnostics.Debug.Print("Remoting: Is Connected");
            }
            #endregion
            if (RemoteControl.IsConnected)
            {
                Console.WriteLine("Loading Server.");
                StreamTv2.SmallWebServer smallServer = new StreamTv2.SmallWebServer();
                Console.WriteLine("Starting Server.");

                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(StartServer), smallServer);

                Console.WriteLine("Server Running, press any key to quit.");
                while (Console.ReadKey(true).Equals(null))
                {
                    System.Threading.Thread.Sleep(1000); // Stop the console tool from using 100% cpu (it works).
                }

                smallServer.Dispose();
            }
            else
            {
                Console.WriteLine("Problem connecting to TvServer, press enter to quit.");
                while (Console.ReadKey(true).Equals(null))
                {
                }
            }
            Console.WriteLine("Ending program.");
        }

        private static void StartServer(object stateInfo)
        {
            StreamTv2.SmallWebServer webServer = (StreamTv2.SmallWebServer)stateInfo;
            webServer.Start(RemoteControl.Instance);
        }
    }
}
